package com.batchengine.velocity;

import com.batchengine.system.SystemMemoryInfo;
import static com.batchengine.util.EnvUtil.getVMFolderLocation;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by  solutions
 * Author: sunil.incubator@yahoo.com
 * Date: Jul 27, 2009
 */
public class VelocityEngine {

    private static final org.apache.velocity.app.VelocityEngine VE = new org.apache.velocity.app.VelocityEngine();
    private static Map<String, Template> templateMap = new HashMap<String, Template>();

    private static final String path = getVMFolderLocation();

    static {
        try {

            Properties p = new Properties();
            p.setProperty("resource.loader", "file");
            p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
            p.setProperty("file.resource.loader.path", path);

            VE.init(p);
            templateMap.put("table.vm", VE.getTemplate("table.vm"));
            templateMap.put("SystemHealth.vm", VE.getTemplate("SystemHealth.vm"));
            templateMap.put("PageRedirect.vm", VE.getTemplate("PageRedirect.vm"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private VelocityEngine() {
    }

    public static String getVelocityFormattedData(String template, Map<String, Object> dataMap) {

        StringWriter writer = new StringWriter();
        VelocityContext context = new VelocityContext();
        Set<String> stringSet = dataMap.keySet();
        for (String s : stringSet) {
            context.put(s, dataMap.get(s));
        }

        try {
            templateMap.get(template).merge(context, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return writer.toString();
    }

    public static String getHtmlTable(String tableHeader, Object[][] data, String[] columnNames) {

        VelocityContext context = new VelocityContext();

        context.put("tableHeader", tableHeader);
        context.put("columnNames", columnNames);
        context.put("tablebody", data);

        StringWriter writer = new StringWriter();
        try {
            templateMap.get("table.vm").merge(context, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }

    public static String reDirectPage(String pageLocation) {

        VelocityContext context = new VelocityContext();
        context.put("pageLocation", pageLocation);
        StringWriter writer = new StringWriter();
        try {
            templateMap.get("PageRedirect.vm").merge(context, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }

    public static void main(String[] args) {

        SystemMemoryInfo systemMemoryInfo = SystemMemoryInfo.getSystemMemoryInfo();
        Map<String, Object> memory = new HashMap<String, Object>();
        memory.put("health", systemMemoryInfo);
        System.out.println(getVelocityFormattedData("SystemHealth.vm", memory));
    }
}
